package tk.redbluelotto.model;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.hibernate.search.annotations.Field;
import org.springframework.security.core.GrantedAuthority;

/**
 * 权限
 * 
 * @author zb
 * 
 */
@Entity
@Table(name = "tb_right")
public class Right extends BaseObject implements Serializable, GrantedAuthority {

	private static final long serialVersionUID = 5935984894455024867L;

	private Long id;

	private String name;

	private String description;

	/**
	 * @return the id
	 */
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	public Long getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(Long id) {
		this.id = id;
	}

	/**
	 * @return the name
	 */
	@Column(nullable = false, length = 32, unique = true)
	@Field
	public String getName() {
		return name;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the description
	 */
	@Column(length = 64)
	@Field
	public String getDescription() {
		return description;
	}

	/**
	 * @param description
	 *            the description to set
	 */
	public void setDescription(String description) {
		this.description = description;
	}

    /**
     * @return the name property (getAuthority required by Acegi's GrantedAuthority interface)
     * @see org.springframework.security.core.GrantedAuthority#getAuthority()
     */
    @Transient
	public String getAuthority() {
		return getName();
	}

	@Override
	public String toString() {
		return new ToStringBuilder(this, ToStringStyle.SIMPLE_STYLE).append(this.name).toString();
	}

	@Override
	public boolean equals(Object o) {
		if (this == o) {
			return true;
		}
		if (!(o instanceof Right)) {
			return false;
		}

		final Right right = (Right) o;

		return !(name != null ? !name.equals(right.name) : right.name != null);

	}

	@Override
	public int hashCode() {
		return (name != null ? name.hashCode() : 0);
	}

}
